package com.dd.census.controller;

import com.dd.census.controller.common.BaseController;
import com.dd.census.entity.Advertisement;
import com.dd.census.entity.Attendance;
import com.dd.census.entity.Staff1;
import com.dd.census.service.AttendanceService;
import com.dd.census.tools.SysException;
import com.dd.census.tools.excel.ExcelExport;
import com.dd.census.tools.excel.ExcelExportCallback;
import com.dd.census.tools.excel.ExcelImport;
import com.dd.census.tools.excel.test.DemoEntity;
import com.dd.census.tools.validator.ValidateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * 控制层
 * AttendanceController
 * @author whn
 * @date 2021/02/20
 */
@Controller
@RequestMapping(value = "rest/attendance")
public class AttendanceController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AttendanceController.class);

    @Autowired
    AttendanceService attendanceService;

    @RequestMapping("selectPage")
    public ResponseEntity<Object> select(@RequestBody Attendance attendance, HttpServletRequest request) {
        try {
            return this.doSuccess(request, attendanceService.selectPage(attendance));
        } catch (SysException e) {
            // 业务异常
            return doFaild(request, e);

        } catch (Exception e) {
            // 系统异常
            logger.error("Internal Server Error", e);
            return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("selectAll")
    public ResponseEntity<Object> selectAll(@RequestBody Attendance attendance, HttpServletRequest request) {
        try {
            return this.doSuccess(request, attendanceService.selectList(attendance));

        } catch (SysException e) {
            // 业务异常
            return doFaild(request, e);

        } catch (Exception e) {
            // 系统异常
            logger.error("Internal Server Error", e);
            return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("/getById/{id}")
    public ResponseEntity<Object> getById(@PathVariable("id") String id, HttpServletRequest request) {
        try {
            return this.doSuccess(request, attendanceService.selectByKey(id));
        } catch (Exception e) {
            // 系统异常
            logger.error("Internal Server Error", e);
            return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("insert")
    public ResponseEntity<Object> insert(HttpServletRequest request, @RequestBody Attendance attendance) {

        try {
            // 基本输入验证
            ValidateUtil.ValidationResult rscheck =ValidateUtil.validate(attendance);
            attendance.setCreateUserId(this.getUserId(request));
            attendance.setCreateTime(new Date());
            if(!rscheck.isHasErrors()) {
                attendanceService.insert(attendance);
                return this.doSuccess(request);
            }else {
                return this.doSuccess(request,rscheck);
            }
        } catch (Exception e) {
            // 系统异常
            logger.error("Internal Server Error", e);
            return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("update")
    public ResponseEntity<Object> update(HttpServletRequest request, @RequestBody Attendance attendance) {

        try {
            // 基本输入验证
            ValidateUtil.ValidationResult rscheck =ValidateUtil.validate(attendance);
            attendance.setUpdateUserId(this.getUserId(request));
            attendance.setUpdateTime(new Date());
            if(!rscheck.isHasErrors()) {
                attendanceService.update(attendance);
                return this.doSuccess(request);
            }else {
                return this.doSuccess(request,rscheck);
            }
        } catch (Exception e) {
            // 系统异常
            logger.error("Internal Server Error", e);
            return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("delete")
    public ResponseEntity<Object> delete(HttpServletRequest request, @RequestBody  Attendance attendance) {

        try {
            attendanceService.delete(attendance);
            return this.doSuccess(request);
        } catch (Exception e) {
            // 系统异常
            logger.error("Internal Server Error", e);
            return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping(value = "uploadExcel", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Object> uploadExcel(HttpServletRequest request, @RequestParam(value = "file") MultipartFile file ,String userId , String adId) {

        try {
            byte [] byteArr=file.getBytes();
            InputStream inputStream = new ByteArrayInputStream(byteArr);
            List<DemoEntity> a = new ExcelImport().getList(inputStream, DemoEntity.class);
            Attendance attendance1 = new Attendance();
            attendance1.setUpdateUserId(this.getUserId(request));
            attendance1.setUpdateTime(new Date());
            attendance1.setAdUserId(Integer.parseInt(userId) );
            attendance1.setAdId(Integer.parseInt(adId));
            return this.doSuccess(request,attendanceService.excelImport(a,attendance1));
        } catch (Exception e) {
            // 系统异常
            logger.error("Internal Server Error", e);
            return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
